package com.cx.anno;

import java.lang.reflect.Field;

@Logger(value = "User", level = "DEBUG")
public class User {
    @NotNull(message = "用户名不能为空")
    private String username;
    @NotNull(message = "昵称不能为空")
    private String nickName;

    public User(String username, String nickName) {
        this.username = username;
        this.nickName = nickName;
    }

    public User() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    // 校验方法
    @ExceptionHandler(value = "validate", message = "校验失败")
    public void validate() throws IllegalAccessException {
        //判断属性上是否有NOtNULL注解，如果有，抛出message异常
        for (Field declaredField : User.class.getDeclaredFields()) {
            declaredField.setAccessible(true); // 设置属性可访问
            if(declaredField.isAnnotationPresent(NotNull.class)){
                NotNull notNull = declaredField.getAnnotation(NotNull.class);
                System.out.println( notNull);
                if(declaredField.get(notNull)==null){
                    System.out.println("该属性无NotNull注解");
                }
                throw new IllegalAccessException(notNull.message());

            }
        }


    }
}